package cn.stylefeng.guns.modular.hx.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.core.util.QRCodeUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicOperateParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicOperateResult;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStoreReturnsRegister;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStorageOffShelfParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStoreReturnsRegisterParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStoreReturnsRegisterResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStoreReturnsRegisterService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.ibatis.javassist.bytecode.ClassFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;


/**
 * 门店退货登记控制器
 *
 * @author ZDW
 * @Date 2024-09-02 10:10:48
 */
@Controller
@RequestMapping("/hxStoreReturnsRegister")
public class HxStoreReturnsRegisterController extends BaseController {

    private String PREFIX = "/hx/storage/hxStoreReturnsRegister";

    @Autowired
    private HxStoreReturnsRegisterService hxStoreReturnsRegisterService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author ZDW
     * @Date 2024-09-02
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/hxStoreReturnsRegister.html";
    }

    /**
     * 新增页面
     *
     * @author ZDW
     * @Date 2024-09-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hxStoreReturnsRegister_add.html";
    }

    /**
     * 编辑页面
     *
     * @author ZDW
     * @Date 2024-09-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hxStoreReturnsRegister_edit.html";
    }

    /**
     * 新增接口
     *
     * @author ZDW
     * @Date 2024-09-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HxStoreReturnsRegisterParam hxStoreReturnsRegisterParam) {
        this.hxStoreReturnsRegisterService.add(hxStoreReturnsRegisterParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author ZDW
     * @Date 2024-09-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxStoreReturnsRegisterParam hxStoreReturnsRegisterParam) {
        this.hxStoreReturnsRegisterService.update(hxStoreReturnsRegisterParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author ZDW
     * @Date 2024-09-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HxStoreReturnsRegisterParam hxStoreReturnsRegisterParam) {
        this.hxStoreReturnsRegisterService.delete(hxStoreReturnsRegisterParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author ZDW
     * @Date 2024-09-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HxStoreReturnsRegisterParam hxStoreReturnsRegisterParam) {
        HxStoreReturnsRegister detail = this.hxStoreReturnsRegisterService.getById(hxStoreReturnsRegisterParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     * @author ZDW
     * @Date 2024-09-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxStoreReturnsRegisterParam hxStoreReturnsRegisterParam,String condition , String startTime, String endTime) {
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStoreReturnsRegisterParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStoreReturnsRegisterParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        User user = userService.getcurrentUser();
        hxStoreReturnsRegisterParam.setDeptId(user.getDeptId());
        hxStoreReturnsRegisterParam.setOperateId(user.getOperateId());
        return this.hxStoreReturnsRegisterService.findPageBySpec(hxStoreReturnsRegisterParam,condition,user.getOperateId());
    }

    @RequestMapping("/toqrCode")
    public String toqrCode() {
        return PREFIX + "/qrCode.html";
    }

    /**
     * 创建二维码
     */
    @RequestMapping(value = "/getqrcode", method = RequestMethod.GET)
    @ResponseBody
    public void generateQRCode4Product(Integer id, String name, HttpServletRequest request, HttpServletResponse response) {
        User user = userService.getcurrentUser();
        BasicOperateParam param = new BasicOperateParam();
        param.setDeptId(user.getDeptId());
        BasicOperateResult basicOperateResult = basicOperateService.findBySpec(param,user.getOperateId());
        if(basicOperateResult != null) {
            try {
                //logo
                File logoFile = new File("");
                String url = "https://jilu56it.top//store_returns?operateId=" + id;
                QRCodeUtil.drawLogoQRCode(logoFile, url, name, 350, 350, request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
